package service;

import mapper.StudentMapper;
import model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.Page;

import java.util.HashMap;
import java.util.List;

    @Service

    @Transactional

    public class StudentService {

        @Autowired

        private StudentMapper studentMapper;


        public void addStudent(Student student) {

            this.studentMapper.insert(student);

        }


        public List<Student> findByName(String name) {

            return this.studentMapper.findByName(name);

        }

        public void delete(int id){

            this.studentMapper.delete(id);

        }


        public Student findById(int id) {

            return this.studentMapper.findById(id);

        }

        public void update(Student student){

            this.studentMapper.update(student);

        }







        //分页数据

        public Page<Student> findByPage(int currentPage) {

            HashMap<String, Object> map = new HashMap<String, Object>();

            Page<Student> page = new Page<Student>();

            //封装当前页数

            page.setCurrPage(currentPage);

            //每页显示的数据

            int pageSize = 4;

            page.setPageSize(pageSize);

            //封装总记录数

            int totalCount = studentMapper.selectCount();

            page.setTotalCount(totalCount);

            //封装总页数

            Integer totalcount = totalCount;

            Integer num = totalcount%pageSize==0? totalcount / pageSize: totalcount / pageSize+1;

            page.setTotalPage(num.intValue());//转换为整数

            map.put("start", (currentPage - 1) * pageSize);//起始的记录值

            map.put("size", page.getPageSize());//获取每个页面的pageSize

            List<Student> list = studentMapper.findByPage(map);

            page.setList(list);

            return page;

        }






        public Page<Student> findByPage_2(int currentPage,HashMap<String, Object> params) {

            HashMap<String, Object> map = new HashMap<String, Object>();

            Page<Student> page = new Page<Student>();

            //封装当前页数

            page.setCurrPage(currentPage);

            //每页显示的数据

            int pageSize = 4;//当前总共是9条，那么可以计算得到3页数据

            page.setPageSize(pageSize);

            //封装带条件查询的总记录数

            int totalCount = studentMapper.selectCount_2(params);

            page.setTotalCount(totalCount);

            //封装总页数

            Integer totalcount = totalCount;

            //计算页数，如果整除，那么直接返回冒号左边的值，否则还需要加1

            Integer num = totalcount%pageSize==0? totalcount / pageSize: totalcount / pageSize+1;

            page.setTotalPage(num.intValue());//转换为整数

            map.put("start", (currentPage - 1) * pageSize);//起始的记录值

            map.put("size", page.getPageSize());//获取每个页面的pageSize

            map.put("code",params.get("code"));

            map.put("username",params.get("username"));

            map.put("age",params.get("age"));

            map.put("gender",params.get("gender"));



            //  map.put("查询条件参数",查询值)   如果后面要做带查询条件的分页，可以在这里添加查询参数，而查询条件参数可以在当前findByPage的方法里加一个参数

            List<Student> list = studentMapper.findByPage_2(map);

            page.setList(list);

            return page;

        }







    }



